package test.tftp;


import java.net.*;
import java.io.*;
import java.util.*;

/**
 * 
 * @author Administrator
 */
public class TftpServerAgent extends Thread
{

    /** Creates a new instance of tftpServerAgent */
    public TftpServerAgent()
    {
        this.m_hashClients = new Hashtable<String, TftpClientAgent>();
    }

    private Hashtable<String, TftpClientAgent> m_hashClients;

    // over the parent's run methord
    public void run()
    {
        try
        {
            DatagramSocket tftpd = new DatagramSocket(69); // tftp server socket
            byte[] buf = new byte[516]; // a buffer for UDP packet
            DatagramPacket dp = new DatagramPacket(buf, 516); // a UDP packet

            DataInputStream din = null;
            TftpClientAgent newClient = null;
            short tftp_opcode = 0; // opcode: the 2 bytes in the front of tftp
                                   // packet
            String tftp_filename = null;
            String tftp_mode = null;
            while (true)
            {
                tftpd.receive(dp); // wait for a client
                buf = dp.getData(); // get the UDP packet data
                din = new DataInputStream(new ByteArrayInputStream(buf));
                tftp_opcode = din.readShort(); // get the opcode
                { // get the filename
                    int fnoffset = 2;
                    int fnlen = 0;
                    while (din.readByte() != 0)
                    {
                        fnlen++; // filename will end with a null char('\0')
                    }
                    tftp_filename = new String(buf, fnoffset, fnlen);

                    // get the mode
                    int mdnoffset = fnoffset + fnlen + 2;
                    int mdnlen = 0;
                    while (din.readByte() != 0)
                    {
                        mdnlen++; // filename will end with a null char('\0')
                    }
                    tftp_mode = new String(buf, mdnoffset, mdnlen);
                }

                switch (tftp_opcode)
                {
                    case 1 :
                        // RRQ
                    case 2 :
                        // WRQ
                        newClient = new TftpClientAgent(dp.getAddress(), dp.getPort(), tftp_opcode, tftp_filename,
                                tftp_mode);
                        newClient.setDaemon(true);
                        newClient.start();

                        this.m_hashClients.put(dp.getAddress().toString() + dp.getPort(), newClient);
                        // System.out.println("debug: Main.main() --> a RRQ thread start ....");
                        break;
                }
                tftpd.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
